import { ref } from 'vue';

import { routerTo } from '@/utils/jump';

export const link_list = [
    {
        id: 1,
        icon: '/icon/home.svg',
        active_icon: '/icon/home-active.svg',
        text: 'Home',
        link: '/tabbar/home',
        click: () => {
            sessionStorage.setItem('active_nav_item', '/tabbar/home');
            routerTo('/tabbar/home');
            active_nav_item.value = link_list[0];
        }
    },
    {
        id: 2,
        icon: '/icon/markets.svg',
        active_icon: '/icon/markets-active.svg',
        text: 'Markets',
        link: '/tabbar/markets',
        click: () => {
            sessionStorage.setItem('active_nav_item', '/tabbar/markets');
            routerTo('/tabbar/markets');
            active_nav_item.value = link_list[1];
        }
    },
    // {
    //     id: 3,
    //     icon: '/icon/asset.svg',
    //     active_icon: '/icon/asset-active.svg',
    //     text: 'Assets',
    //     link: '/tabbar/asset',
    //     click: () => {
    //         sessionStorage.setItem('active_nav_item', '/tabbar/asset');
    //         routerTo('/tabbar/asset');
    //         active_nav_item.value = link_list[2];
    //     }
    // },
    {
        id: 3,
        icon: '/icon/trade.svg',
        active_icon: '/icon/trade-active.svg',
        text: 'Trade',
        link: '/tabbar/trade',
        click: () => {
            sessionStorage.setItem('active_nav_item', '/tabbar/trade');
            routerTo('/tabbar/trade');
            active_nav_item.value = link_list[2];
        }
    },
    {
        id: 4,
        icon: '/icon/deposit.svg',
        active_icon: '/icon/deposit-active.svg',
        text: 'Profile',
        link: '/tabbar/profile',
        click: () => {
            sessionStorage.setItem('active_nav_item', '/tabbar/profile');
            routerTo('/tabbar/profile');
            active_nav_item.value = link_list[3];
        }
    },
];

export const active_nav_item = ref(link_list[0]);

export function get_active_nav_item() {
    // 获取当前页面的path
    let path = window.location.pathname;
    if (path === '/' || path === '') {
        // 直接指定为首页
        path = '/tabbar/home';
    } else if (path.length > 1 && path.endsWith('/')) {
        // 去掉最后一个 '/' 字符
        path = path.slice(0, -1);
    }
    // 匹配路由
    for (let i = 0; i < link_list.length; i++) {
        let item = link_list[i];
        if (item.link == path) {
            active_nav_item.value = item;
            break;
        }
    }
}